package com.mylearn.myblog.controller;

import com.mylearn.myblog.entity.Type;
import com.mylearn.myblog.service.TypeService;
import com.mylearn.myblog.util.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/type")
@Api(value = "分类接口",description = "分类接口")
public class TypeController {

	@Autowired
	private TypeService typeService;

	@ApiOperation(value = "分页查询分类",notes = "分页查询分类")
	@RequestMapping(value = "/types",method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", dataType = "Integer", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页显示数", dataType = "Integer", paramType = "query", required = true)
	})
	public BaseResult types(@RequestParam Integer page,@RequestParam Integer pageSize){
		if(page<1){
			return BaseResult.notOk();
		}
		Pageable pageable = PageRequest.of(page-1,pageSize,Sort.Direction.DESC,"id");
		Page<Type> types = typeService.listType(pageable);
		return BaseResult.ok(types);
	}

	@ApiOperation(value = "新增分类",notes = "新增分类")
	@RequestMapping(value = "/addtype",method = RequestMethod.POST)
	public BaseResult addtype(@RequestBody Type type){
		Type type1 = typeService.getTypeByName(type.getName());
		if (type1 != null){
			return BaseResult.notOk();
		}
		Type t = typeService.saveType(type);
		if (t == null){
			return BaseResult.notOk();
		}
		return BaseResult.ok(t);
	}

	@ApiOperation(value = "根据id查询分类",notes = "根据id查询分类")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "分类id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/editInput/{id}",method = RequestMethod.GET)
	public BaseResult editInput(@PathVariable Long id){
		return BaseResult.ok(typeService.getType(id));
	}

	@ApiOperation(value = "修改/更新分类",notes = "修改/更新分类")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "分类id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/editType/{id}",method = RequestMethod.POST)
	public BaseResult editType(@RequestBody Type type,@PathVariable Long id){
		Type type1 = typeService.getTypeByName(type.getName());
		if (type1 != null){
			return BaseResult.notOk();
		}
		Type t = typeService.updateType(id,type);
		if (t == null){
			return BaseResult.notOk();
		}
		return BaseResult.ok(t);
	}

	@ApiOperation(value = "根据id删除分类",notes = "根据id删除分类")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "分类id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
	public BaseResult delete(@PathVariable Long id){
		Type type  = typeService.getType(id);
		if (type != null){
			typeService.deleteType(id);
			return BaseResult.ok();
		}
		return BaseResult.notOk();
	}

	@ApiOperation(value = "查询全部分类",notes = "查询全部分类")
	@RequestMapping(value = "/listAll",method = RequestMethod.GET)
	public BaseResult listAll(){
		return BaseResult.ok(typeService.listAll());
	}
}
